﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using BaseTypesLib.TypeConverters;
using System.Xml.Linq;
using BaseTypesLib.UiTypeEditors;
using System.Drawing.Design;

namespace BaseTypesLib.Model.Fonts
{
    public enum FieldFontStyle { Plain, Italic, Bold, Underlined }

    [Editor(typeof(FontEditor), typeof(UITypeEditor))]
    public class Font : INotifyPropertyChanged
    {
        private int height;
        private FieldFontStyle style;
        private string family;

        public int Height
        {
            get { return height; }
            set
            {
                if (height != value)
                {
                    height = value;
                    OnPropertyChanged("Height");
                }
            }
        }
        public FieldFontStyle Style
        {
            get { return style; }
            set
            {
                if (style != value)
                {
                    style = value;
                    OnPropertyChanged("Style");
                }
            }
        }
        public string Family
        {
            get { return family; }
            set
            {
                if (family != value)
                {
                    family = value;
                    OnPropertyChanged("Family");
                }
            }
        }

        #region inpc
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
        #endregion

        public XElement GetXml()
        {
            XElement elem = new XElement(GetType().Name);
            if (Height > 0)
                elem.SetAttributeValue("Size", Height);
            elem.SetAttributeValue("Style", Style);
            if (Family != null && Family.Trim().Length > 0)
                elem.SetAttributeValue("Family", Family.Trim());
            return elem;
        }
        public static Font FromXml(XElement elem)
        {
            Font res = new Font();
            var size = elem.Attributes("Size").FirstOrDefault();
            if (size != null) res.Height = (int)size;
            var style = elem.Attributes("Style").FirstOrDefault();
            if (style != null)
                res.Style = (FieldFontStyle)Enum.Parse(typeof(FieldFontStyle), style.Value);
            var fam = elem.Attributes("Family").FirstOrDefault();
            if (fam != null)
                res.Family = fam.Value;
            return res;
        }

        public override string ToString()
        {
            return string.Format("{0}, {1}", Style, Height);
        }
    }
}
